/**
 * 交互式UT生成的消息协议定义
 */

export type GenerationMode = 'agent' | 'qa';

export type StepStatus = 'pending' | 'in-progress' | 'completed' | 'error';

// ============== 后端 -> 前端消息 ==============

export type StreamMessage =
    | InitMessage
    | StepStartMessage
    | StepProgressMessage
    | StepCompleteMessage
    | ThinkingMessage
    | CodeChunkMessage
    | QuestionMessage
    | ConfirmationRequestMessage
    | ErrorMessage
    | GenerationCompleteMessage
    | InfoMessage
    | ChatResponseStartMessage
    | ChatResponseEndMessage
    | FileActionMessage
    | FileInfoMessage;

export interface InitMessage {
    type: 'init';
    mode: GenerationMode;
    sessionStarted: boolean;
}

export interface StepStartMessage {
    type: 'step-start';
    step: number;
    totalSteps: number;
    title: string;
    description?: string;
}

export interface StepProgressMessage {
    type: 'step-progress';
    step: number;
    progress: number; // 0-100
    message: string;
}

export interface StepCompleteMessage {
    type: 'step-complete';
    step: number;
    data: any;
    summary?: string;
}

export interface ThinkingMessage {
    type: 'thinking';
    content: string;
}

export interface CodeChunkMessage {
    type: 'code-chunk';
    content: string;
    language?: string;
}

export interface QuestionMessage {
    type: 'question';
    questionId: string;
    question: string;
    options?: string[];
    allowCustomInput?: boolean;
}

export interface ConfirmationRequestMessage {
    type: 'confirmation-request';
    requestId: string;
    message: string;
    options: string[];
    defaultOption?: number;
}

export interface ErrorMessage {
    type: 'error';
    message: string;
    details?: string;
}

export interface GenerationCompleteMessage {
    type: 'generation-complete';
    success: boolean;
    message: string;
    testFilePath?: string;
    gnFilePath?: string;
}

export interface InfoMessage {
    type: 'info';
    level: 'info' | 'warning' | 'success';
    message: string;
}

export interface ChatResponseStartMessage {
    type: 'chat-response-start';
}

export interface ChatResponseEndMessage {
    type: 'chat-response-end';
}

export interface FileActionMessage {
    type: 'file-action';
    actionId: string;
    fileType: 'test' | 'gn';
    filePath: string;
    displayPath: string;
    content?: string;  // 文件内容（如果需要预览）
    action: 'create' | 'modify';
}

export interface FileInfoMessage {
    type: 'file-info';
    fileType: 'source-gn' | 'test-gn' | 'test-file';
    filePath: string;
    displayPath: string;
    label: string;  // 显示标签，如 "源文件的 GN 配置"
}

// ============== 前端 -> 后端消息 ==============

export type UserMessage =
    | WebviewReadyMessage
    | AnswerMessage
    | ConfirmationMessage
    | CancelMessage
    | ModifyMessage
    | ApplyMessage
    | ChatMessageType
    | FileActionResponseMessage;

export interface WebviewReadyMessage {
    type: 'webview-ready';
}

export interface AnswerMessage {
    type: 'answer';
    questionId: string;
    answer: string;
}

export interface ConfirmationMessage {
    type: 'confirmation';
    requestId: string;
    choice: string;
    choiceIndex: number;
}

export interface CancelMessage {
    type: 'cancel';
}

export interface ModifyMessage {
    type: 'modify';
    field: string;
    value: any;
}

export interface ApplyMessage {
    type: 'apply';
    testCode: string;
    gnChanges?: any;
}

export interface ChatMessageType {
    type: 'chat-message';
    message: string;
}

export interface FileActionResponseMessage {
    type: 'file-action-response';
    actionId: string;
    decision: 'accept' | 'reject';
}

// ============== 会话状态 ==============

export interface UTGenerationSession {
    sessionId: string;
    mode: GenerationMode;
    sourceFile: string;
    functionName: string;
    startTime: number;
    currentStep: number;
    totalSteps: number;
    status: 'initializing' | 'running' | 'waiting-user' | 'completed' | 'cancelled' | 'error';
}

// ============== 步骤数据 ==============

export interface StepData {
    step: number;
    title: string;
    status: StepStatus;
    progress: number;
    messages: Array<{
        id: string;
        content: string;
        timestamp: number;
    }>;
    data?: any;
}

// ============== 分析结果 ==============

export interface CodeAnalysisResult {
    functionName: string;
    className?: string;
    parameters: Array<{
        name: string;
        type: string;
    }>;
    returnType: string;
    isStatic: boolean;
    isTemplate: boolean;
    complexity: 'simple' | 'medium' | 'complex';
}

export interface DependencyAnalysisResult {
    includes: string[];
    externalDependencies: Array<{
        name: string;
        type: 'class' | 'function' | 'library';
        mockable: boolean;
        reason?: string;  // LLM 分析时提供原因
    }>;
    suggestions: string[];
}

export interface GNAnalysisResult {
    gnFilePath: string;
    testTargets: Array<{
        name: string;
        type: string;
        sources?: string[];  // LLM 分析时可能提取的源文件列表
    }>;
    recommendation: {
        action: 'add-to-existing' | 'create-new';
        targetName: string;
        testDirectory: string;
        reason?: string;  // LLM 分析时提供推荐理由
    };
    buildInfo?: {  // LLM 分析时的额外构建信息
        subsystem?: string;
        part?: string;
        dependencies?: string[];
    };
}



